package com.example.c201901090104;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class ShouyeFragment extends Fragment {
    private String[] tabs = {"头条", "国内", "娱乐", "体育", "军事", "科技", "财经","时尚"};
    private String[] tabs_name={"top", "guonei", "yule", "tiyu", "junshi", "keji", "caijing","shishang"};
    private List<TabFragment> tabFragmentList = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        tabFragmentList.clear();
        View view = inflater.inflate(R.layout.fragment_shouye, container, false);
        TabLayout tabLayout = view.findViewById(R.id.tab_layout);
        ViewPager viewPager = view.findViewById(R.id.view_pager);
        //添加tab
        for (int i = 0; i < tabs.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));
            tabFragmentList.add(TabFragment.newInstance(tabs_name[i]));
        }
        viewPager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return tabFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return tabFragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });

        //设置TabLayout和ViewPager联动
        tabLayout.setupWithViewPager(viewPager,false);
        return view;
    }

    /*
    RecyclerView recyclerView;
    List<News.ResultDTO.DataDTO> newslist = new ArrayList();
    MyAdapter myAdapter;
    SmartRefreshLayout smartRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_shouye, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = getActivity().findViewById(R.id.recyclerView);
        smartRefreshLayout = getActivity().findViewById(R.id.smartRefreshLayout);
        newslist.clear();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams("https://www.fastmock.site/mock/c1f4516733e15a014ac10708d93a43f5/android2/news");//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Log.i("Main", result);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                News news = gson.fromJson(result, News.class);
                                for (int i = 0; i < news.result.data.size(); i++) {
                                    newslist.add(news.result.data.get(i));
                                }
                                myAdapter = new MyAdapter();
                                recyclerView.setAdapter(myAdapter);
                                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

                            }
                        });
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        });
        thread.start();
        smartRefreshLayout.setRefreshFooter(new BallPulseFooter(getActivity()));
        smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(getActivity()));
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                newslist.clear();
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams("https://www.fastmock.site/mock/c1f4516733e15a014ac10708d93a43f5/android2/news");//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Gson gson = new Gson();
                        News news = gson.fromJson(result, News.class);
                        for (int i = 0; i < news.result.data.size(); i++) {
                            newslist.add(news.result.data.get(i));
                        }
                        myAdapter.notifyDataSetChanged();
                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onCancelled(CancelledException cex) {

                    }
                    @Override
                    public void onFinished() {

                    }
                });
                refreshLayout.finishRefresh(1000);
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //newslist.clear();
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams("https://www.fastmock.site/mock/c1f4516733e15a014ac10708d93a43f5/android2/news");//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Gson gson = new Gson();
                        News news = gson.fromJson(result, News.class);
                        for (int i = 0; i < news.result.data.size(); i++) {
                            newslist.add(news.result.data.get(i));
                        }
                        myAdapter.notifyDataSetChanged();
                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onCancelled(CancelledException cex) {

                    }
                    @Override
                    public void onFinished() {

                    }
                });
                refreshLayout.finishLoadMore(1000);
            }
        });
    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.list1_item, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.nameTv.setText(newslist.get(position).title);
            holder.contentTv.setText(newslist.get(position).author_name);
            holder.timeTv.setText(newslist.get(position).date);
            Glide.with(getActivity())
                    .load(newslist.get(position).thumbnail_pic_s).transform(new GlideRectRound(getActivity(), 5))
                    .into(holder.imageView);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), Click_news.class);
                    intent.putExtra("url", newslist.get(position).url);
                    startActivity(intent);
                    //Toast.makeText(getActivity(),"点击",Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return newslist.size();
        }


    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView contentTv;
        TextView timeTv;
        ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.titleView);
            contentTv = itemView.findViewById(R.id.contentView);
            timeTv = itemView.findViewById(R.id.timeView);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }*/
    /*
    RecyclerView recyclerView;
    List<Chat.GoodsDTO> chatList1 = new ArrayList<>();
    MyAdapter myAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_message, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = getActivity().findViewById(R.id.recyclerview);
        chatList1.clear();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                x.Ext.init(getActivity().getApplication());//框架初始化
                RequestParams requestParams = new RequestParams("https://www.fastmock.site/mock/192580c9a895f31a51d4f85ea1192867/android1/android1");//请求地址构造
                x.http().get(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        //Log.i("Main", result);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                Chat chat = gson.fromJson(result, Chat.class);
                                for (int i = 0; i < chat.goods.size(); i++) {
                                    chatList1.add(chat.goods.get(i));
                                }
                                myAdapter = new MyAdapter();
                                recyclerView.setAdapter(myAdapter);
                                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

                            }
                        });
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        });
        thread.start();
    }


    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_chat, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.nameTv.setText(chatList1.get(position).name);
            holder.contentTv.setText(chatList1.get(position).shopname);
            holder.timeTv.setText(chatList1.get(position).price);
            Glide.with(getActivity())
                    .load(chatList1.get(position).head)
                    .into(holder.imageView);
        }

        @Override
        public int getItemCount() {
            return chatList1.size();
        }
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView contentTv;
        TextView timeTv;
        ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.nametextview);
            contentTv = itemView.findViewById(R.id.contenttextview);
            timeTv = itemView.findViewById(R.id.timetextview);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }

     */
}